package com.lingjtx.iot.network.common;

import com.lingjtx.common.core.RedisOptions;
import com.lingjtx.common.core.constant.RedisConfig;
import com.lingjtx.common.core.handle.Injector;
import com.lingjtx.common.core.model.iot.DeviceBaseInfo;

import java.util.HashMap;
import java.util.Map;

public class DataCache {

    public static DeviceBaseInfo getDeviceBaseInfo(String productId, String mac, RedisOptions redis, Injector dao) {
        DeviceBaseInfo entity = (DeviceBaseInfo) redis.hmGet(RedisConfig.HASHKEY_DEVICE_BASE_INFO + productId, mac);
        if (entity == null) {
            Map<String, String> params = new HashMap<>();
            params.put("productId", productId);
            params.put("mac", mac);
            entity = dao.getDeviceBaseInfo(params);
            if (entity != null) {
                redis.hmPut(RedisConfig.HASHKEY_DEVICE_BASE_INFO + productId, mac, entity);
                redis.expire(RedisConfig.HASHKEY_DEVICE_BASE_INFO + productId, RedisConfig.TIMEOUT_3_HOUR);
                return entity;
            }
        }
        return entity;
    }
}
